package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

///system/user/list.do
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {


    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list",name = "分页展示用户列表数据")
    public String findAll(@RequestParam(value = "page",required=false,defaultValue ="1" )Integer pageNum,
                          @RequestParam(required=false,defaultValue ="10" )Integer pageSize){

//        分页查询用户数据时要区分是哪个企业的
        PageInfo pageInfo = userService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageInfo); //这个“page” 固定不能修改 因为页面上要的就是page这个变量名
        return "system/user/user-list";
    }
    @RequestMapping(value = "/toAdd",name = "进入到新增用户的页面")
    public String toAdd(){
//        查询当前企业的所有部门数据
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);

        return "system/user/user-add";
    }


    @RequestMapping(value = "/edit",name = "保存用户的方法")
    public String edit(User user){
//        判断user中id是否为空，如果为空就是新增反之就是修改
        if(StringUtils.isEmpty(user.getId())){//如果为空
            String id = UUID.randomUUID().toString();
            user.setId(id);
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
            userService.save(user);
        }else{
            userService.update(user);
        }
        return "redirect:/system/user/list.do"; //一定要重定向到列表页面
    }

    @RequestMapping(value = "/toUpdate",name = "进入到修改用户的页面")
    public String toUpdate(String id){
        User user = userService.findById(id);
        request.setAttribute("user",user);

//        查询当前企业的所有部门数据
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);

        return "system/user/user-add";
    }

    @RequestMapping(value = "/delete",name = "删除用户的方法")
    public String delete(String id){
        userService.delete(id);
        return "redirect:/system/user/list.do"; //一定要重定向到列表页面
    }

    @RequestMapping(value = "/roleList",name = "进入到给用户分配角色页面")
    public String roleList(String id){ //id 用户的id
//        查询当前用户有哪些角色id  select role_id from pe_role_user where user_id=?
       List<String> roleIdList =  userService.findRoleIdsByUserId(id);
//       把list中的数据转成字符串
//        StringBuffer userRoleStr = new StringBuffer();
//        for (String roleId : roleIdList) {
//            userRoleStr.append(roleId).append(",");
//        }
        String userRoleStr = roleIdList.stream().collect(Collectors.joining(","));
        request.setAttribute("userRoleStr",userRoleStr); //userRoleStr 当前用户角色的id拼凑的字符串 用逗号隔开
//        查询当前用户，放到request中
        User user = userService.findById(id);
        request.setAttribute("user",user);
//        查询当前企业的所有角色
        List<Role> roleList = roleService.findAll(getCompanyId());
//                放到request中
        request.setAttribute("roleList",roleList);
//                跳转页面user-role.jsp
        return "system/user/user-role";
    }

    @RequestMapping(value = "/changeRole",name = "保存用户分配角色的方法")
    public String changeRole(String userid,String[] roleIds){
        userService.changeRole(userid,roleIds); //用户分配角色方法
        return "redirect:/system/user/list.do"; //一定要重定向到列表页面
    }

}
